﻿DECLARE @Var_CallNo      VARCHAR(100) SET @Var_CallNo      = @CallNo_In   
DECLARE @Var_CallName    VARCHAR(100) SET @Var_CallName    = @CallName_In 

DECLARE @Var_RegistId    INT          SET @Var_RegistId    = @RegistId_In
DECLARE @Var_PayMoney    MONEY        SET @Var_PayMoney    = @PayMoney_In
DECLARE @Var_PatientId   VARCHAR(100) SET @Var_PatientId   = @PatientId_In
DECLARE @Var_TransNo     VARCHAR(100) SET @Var_TransNo     = @TransNo_In
DECLARE @Var_BanktransNo VARCHAR(100) SET @Var_BanktransNo = @BanktransNo_In
DECLARE @Var_PayDeviceId VARCHAR(100) SET @Var_PayDeviceId = @PayDeviceId_In
DECLARE @Var_PayWay      INT          SET @Var_PayWay      = @PayWay_In
DECLARE @Var_YBFlag      INT          SET @Var_YBFlag      = @YBFlag_In

DECLARE @HIS_DeptCode            VARCHAR(100)
DECLARE @HIS_DeptName            VARCHAR(100)
DECLARE @HIS_DoctCode            VARCHAR(100)
DECLARE @HIS_DoctName            VARCHAR(100)
DECLARE @HIS_CheckGroupCode      VARCHAR(100)
DECLARE @HIS_CheckGroupName      VARCHAR(100)
--DECLARE @HIS_DoctTitleCode       VARCHAR(20)
--DECLARE @HIS_DoctTitleName       VARCHAR(100)
DECLARE @HIS_PatientName         VARCHAR(100)
DECLARE @HIS_TimeInterval        VARCHAR(100)
DECLARE @HIS_PrePayMoney         MONEY
DECLARE @HIS_PreRegistMoney      MONEY
DECLARE @HIS_PrePatientId        VARCHAR(100)
DECLARE @HIS_OPERCODE            VARCHAR(100)
DECLARE @HIS_IsFinish            BIT
DECLARE @HIS_PreRegistDate       DATETIME
DECLARE @HIS_PreOperDate         DATETIME
DECLARE @HIS_CurrentDate         DATETIME
DECLARE @HIS_SourceNumber        VARCHAR(100)
DECLARE @HIS_TransFlow           VARCHAR(100)
DECLARE @HIS_TransactionTime     DATETIME
DECLARE @HIS_CurrentNumber       VARCHAR(100)
DECLARE @HIS_QueueNumber         VARCHAR(100)
DECLARE @HIS_ClinicRange         VARCHAR(100)
DECLARE @HIS_Sex                 INT
DECLARE @HIS_Address             VARCHAR(100)
DECLARE @HIS_Duty                VARCHAR(100)
DECLARE @HIS_FPNum               INT
DECLARE @HIS_MZNUM               INT
DECLARE @HIS_GROUPKEYNO          NUMERIC(18)
DECLARE @HIS_USERID              NUMERIC(18)
DECLARE @HIS_AP_OrderSheet_Id    INT

IF CAST(GETDATE() AS TIME(0))<'06:00'
BEGIN
    SELECT -1 AS RESULT,'00:00-06:00这段时间内禁止线上挂号，请到窗口进行挂号！' AS RESULTTEXT
    RETURN
END

SET @HIS_CurrentDate = GETDATE()

   SELECT @HIS_DeptCode        = A.KSCode      
         ,@HIS_DeptName        = A.KSName
         ,@HIS_DoctCode        = A.YSCode
         ,@HIS_DoctName        = A.YSName
         ,@HIS_CheckGroupCode  = A.CheckGroupCode
         ,@HIS_CheckGroupName  = A.CheckGroupName
         ,@HIS_PrePatientId    = A.PatientId
         ,@HIS_PatientName     = A.PatientName   
         ,@HIS_TimeInterval    = A.TimeInterval
         ,@HIS_PreRegistMoney  = A.RegistMoney
         ,@HIS_PrePayMoney     = A.PayMoney
         ,@HIS_PreRegistDate   = A.RegistDate
         ,@HIS_PreOperDate     = A.PreOperDate
         ,@HIS_IsFinish        = A.IsFinish
         ,@HIS_SourceNumber    = A.SourceNumber   
         ,@HIS_TransFlow       = A.TransFlow      
         ,@HIS_TransactionTime = A.TransactionTime
         ,@HIS_CurrentNumber   = A.CurrentNumber  
         ,@HIS_QueueNumber     = A.QueueNumber    
         ,@HIS_ClinicRange     = A.ClinicRange    
         ,@HIS_Sex             = A.Sex            
         --,@HIS_DeptName        = A.DeptName       
         ,@HIS_Address         = A.Address        
         --,@HIS_DoctorName      = A.DoctorName     
         ,@HIS_Duty            = A.Duty           
         ,@HIS_FPNum           = A.FPNum          
     FROM AP_PreRegist_DongRuan A(NOLOCK) 
 WHERE Id = @Var_RegistId AND IsReservation = 0

IF @HIS_DeptCode IS NULL
BEGIN
    SELECT -1 AS RESULT, '未找到相应的预挂号记录' AS RESULTTEXT
    RETURN
END
--如果是挂号成功的则直接在这里查询出相应的结果并进行返回
IF @HIS_IsFinish = 1 
BEGIN
    IF NOT EXISTS(SELECT * FROM MZCheck (NOLOCK) WHERE FPnum = @HIS_FPNum)
    BEGIN
        SELECT -1 AS RESULT, '未找到相应的费用明细' AS RESULTTEXT
        RETURN
    END
    SELECT 1 AS Result,'成功' AS ResultText,CHECKNAME,CHECKPRICE
           ,@HIS_SourceNumber    AS SourceNumber   
           ,@HIS_TransFlow       AS TransFlow      
           ,@HIS_TransactionTime AS TransactionTime
           ,@HIS_CurrentNumber   AS CurrentNumber  
           ,@HIS_QueueNumber     AS QueueNumber    
           ,@HIS_ClinicRange     AS ClinicRange    
           ,@HIS_PrePatientId    AS PatientId      
           ,@HIS_PatientName     AS Name           
           ,@HIS_Sex             AS Sex            
           ,@HIS_DeptName        AS DeptName       
           ,@HIS_Address         AS Address        
           ,@HIS_DoctName        AS DoctorName     
           ,@HIS_Duty            AS Duty           
           ,@HIS_FPNum           AS FPNum          
      FROM MZCHECK (NOLOCK) WHERE FPNum = @HIS_FPNum
    RETURN
END
--这里是AP_PreRegist_DongRuan中IsFinish为0走的分支
IF @HIS_PreOperDate < GETDATE()
BEGIN
    SELECT -1 AS RESULT,'预挂号已过期，请重新预挂号！' AS RESULTTEXT
    RETURN
END

IF @Var_PatientId <> @HIS_PrePatientId 
BEGIN
    SELECT -1 AS RESULT,'预挂号的PatientId和此次确认挂号的PatientId不匹配！' AS RESULTTEXT
    RETURN
END

IF @Var_PayMoney <> @HIS_PrePayMoney 
BEGIN
    SELECT -1 AS RESULT,'预挂号的金额与此次确认挂号的金额不匹配！' AS RESULTTEXT
    RETURN
END;

SELECT @HIS_OperCode=CODE FROM YSCODE(NOLOCK) WHERE NAME=@Var_CallName
IF @HIS_OperCode IS NULL
BEGIN
    SELECT -1 AS RESULT,'未找到操作员代码，请联系开发商！' AS RESULTTEXT
    RETURN
END

IF NOT EXISTS(SELECT * FROM CHECKGROUPCODE (NOLOCK) WHERE GROUPCODE=@HIS_CheckGroupCode AND GROUPPRICE=@Var_PayMoney)
BEGIN
    SELECT -1 AS RESULT,'未找到排班类型代码对应的挂号或传入的金额有误！' AS RESULTTEXT
    RETURN
END

IF EXISTS(SELECT Id FROM AP_OrderSheet(NOLOCK) WHERE ORDERFROMTYPE=@Var_CallNo AND (TRANSNO=@Var_TransNo or DELTRANSNO=@Var_TransNo))
BEGIN
    SELECT -1 AS RESULT,'流水号重复！' AS RESULTTEXT
    RETURN
END

declare @HIS_TotalNum int
declare @HIS_TotalNum2 int
declare @HIS_OtherAlreadyCount int
declare @HIS_HosAlreadyCount int
declare @HIS_WebServiceOffLineCount int

SELECT @HIS_TotalNum=totnum,@HIS_TotalNum2=TOTNUM2 FROM MZSCHEDULE (NOLOCK) WHERE KSCODE=@HIS_DeptCode AND YSCODE=@HIS_DoctCode AND TIMEINTERVAL=@HIS_TimeInterval AND
CHECKGROUPCODE=@HIS_CheckGroupCode AND
CHECKMONEY1+CHECKMONEY2=@Var_PayMoney AND
(STATUS IS NULL OR STATUS=2) AND TIMEINTERVAL IS NOT NULL AND WEEKDAY=DATEPART(WEEKDAY,@HIS_CurrentDate)

IF @HIS_TotalNum IS NULL
BEGIN
    SELECT -1 AS RESULT,'挂号目标不正确，请核对，有可能是医院调整了排班信息！' AS RESULTTEXT
    RETURN
END

IF @HIS_DoctCode = '*'
BEGIN
    SET @HIS_DoctCode = ''
    SET @HIS_DoctName = ''
END

SELECT @HIS_OtherAlreadyCount=COUNT(*) FROM AP_RESERVATIONSHEET(NOLOCK) 
 WHERE DEPTCODE=@HIS_DeptCode AND DOCTCODE=@HIS_DoctCode AND 
       TID=@HIS_TimeInterval AND RESERVATIONDATE = CAST(@HIS_CurrentDate AS DATE) AND DELDATE IS NULL
SELECT @HIS_WebServiceOffLineCount=SUM(CASE WHEN mznum>0 THEN 1 ELSE -1 END) 
  FROM MZRegisterSheet A(NOLOCK) JOIN AP_ORDERSHEET B(NOLOCK) ON A.FPNum=B.FPNum 
 WHERE B.Type=1 AND KSCode=@HIS_DeptCode AND YSCode=@HIS_DoctCode AND A.DELDATE IS NULL AND 
       CAST(REGISTERDATE AS DATE)=CAST(@HIS_CurrentDate AS DATE)
SELECT @HIS_HosAlreadyCount=SUM(CASE WHEN mznum>0 THEN 1 ELSE -1 END) 
  FROM MZRegisterSheet(NOLOCK) 
 WHERE KSCode=@HIS_DeptCode AND YSCode=@HIS_DoctCode AND DELDATE IS NULL AND 
       CAST(REGISTERDATE AS DATE)=CAST(@HIS_CurrentDate AS DATE)

SET @HIS_TotalNum2=ISNULL(@HIS_TotalNum2,0)

IF @HIS_TotalNum>0
BEGIN
    IF (@HIS_TotalNum-@HIS_HosAlreadyCount<=0) OR 
       (@HIS_OtherAlreadyCount + @HIS_WebServiceOffLineCount >= @HIS_TotalNum2)
    BEGIN
        SELECT -1 AS RESULT,'该医师号源已挂完了，请稍后再试！' AS RESULTTEXT
        RETURN
    END
END

EXEC GETUNIQUENO 0,@NEWUNIQUENO=@HIS_USERID OUTPUT
EXEC GETUNIQUENO 14,@NEWUNIQUENO=@HIS_GROUPKEYNO OUTPUT
EXEC GETUNIQUENO 22,@NEWUNIQUENO=@HIS_MZNUM OUTPUT
EXEC GETUNIQUENO 7,@NEWUNIQUENO=@HIS_FPNUM OUTPUT

INSERT INTO _MZCHECK(CHECKNO,CHECKNAME,  UNITNAME,CHECKPRICE,CHECKCOUNT,CHECKMONEY,       YSCODE,       YSNAME,  KMNAME,  KMCODE,     YSKSCODE,     YSKSNAME,USERID     ,        PROCDATE,     FSKSCODE,     FSKSNAME,CHECKLB,CHECKLBNAME,          GROUPCODE,          GROUPNAME,   GROUPPRICE,GROUPCOUNT,GROUPKEYNO)
SELECT             CHECKCODE,CHECKNAME,B.UNITNAME,CHECKPRICE,CHECKCOUNT,CHECKMONEY,@HIS_DoctCode,@HIS_DoctName,B.KMNAME,B.KMCODE,@HIS_DeptCode,@HIS_DeptName,@HIS_USERID,@HIS_CurrentDate,@HIS_DeptCode,@HIS_DeptName,CHECKLB,CHECKLBNAME,@HIS_CheckGroupCode,@HIS_CheckGroupName,@Var_PayMoney,1         ,@HIS_GROUPKEYNO 
  FROM CHECKGROUPSHEET A(NOLOCK) JOIN CHECKCODE B(NOLOCK) ON A.CheckCode=B.Code
 WHERE GROUPCODE = @HIS_CheckGroupCode

DECLARE @RESULTTEXT VARCHAR(500)
DECLARE @RESULT INT
EXEC @RESULT=SAVEMZFP
     @fpnum=@HIS_FPNUM
    ,@userid=@HIS_USERID
    ,@fpname=@HIS_PatientName
    ,@fpmoney=@Var_PayMoney
    ,@opername=@Var_CallName
    ,@retval=@RESULTTEXT OUTPUT
    ,@currentdate=@HIS_CURRENTDATE
    ,@oldfpnum=0
    ,@ybflag=9
    ,@ybnum=NULL
    ,@jfcardid=null
    ,@roundflag=null
    ,@ybareacode=null
    ,@ybareaname=null
    ,@zfmoney1=@Var_PayMoney
    ,@zfmoney2=null
    ,@roundmoney=null
    ,@mznum=@HIS_MZNUM
    ,@yldyname='自费'
    ,@fpnum_tmp=null
    ,@yberror=null
    ,@patientid=@HIS_PrePatientId
    ,@transno=@Var_TransNo
    ,@reserveflag=9
    ,@zfmoney3=null
    ,@InvoiceNum=null
    ,@InvoiceOperID=null
    ,@Sex=null
    ,@allownotPrnFP=1
    ,@CreateCardFlag=null
    ,@zfmoney4=null
    ,@YSCode_Pay=NULL
    ,@YSName_Pay=NULL

IF @RESULT <> 0
BEGIN
    --'执行SAVEMZFPSTOREDPROC错误，错误代码为：'
    SELECT -1 AS RESULT, @RESULTTEXT AS RESULTTEXT
    RETURN
END

declare @CallNum varchar(20)
if exists(select * from kscode (nolock) where code=@HIS_DeptCode and DepartmentCode is NOT null AND DepartmentCode <> '')
BEGIN
    DECLARE @t_ret char(10)
    EXEC MZFZ @MZNum=@HIS_MZNUM,@flag=0,@ysname=@HIS_DOCTNAME,@linenum=@t_ret output
    SELECT @CallNum=CallNum FROM mzregistersheet (nolock) where mznum=@HIS_MZNUM
END

INSERT INTO AP_ORDERSHEET(FPNUM,[TYPE],                        [DATE],                           [TIME],      [MONEY],ORDERFROMTYPE,ORDERFROMNAME,        OPERDATE,TRANSNO     ,        DEVICEID,BANKTRANSNO     ,PayWay)
     VALUES         (@HIS_FPNUM,1     ,CAST(@HIS_CurrentDate AS DATE),CAST(@HIS_CurrentDate AS TIME(0)),@Var_PayMoney,  @Var_CallNo,@Var_CallName,@HIS_CURRENTDATE,@Var_TransNo,@Var_PayDeviceId,@Var_BanktransNo,@Var_PayWay)

    
SELECT @HIS_AP_OrderSheet_Id = Id FROM dbo.AP_OrderSheet WHERE @@ROWCOUNT > 0 AND Id = scope_identity()  

UPDATE AP_PreRegist_DongRuan
   SET sourceNumber    = CONVERT(VARCHAR(20),@HIS_AP_OrderSheet_Id)
      ,transFlow       = CONVERT(VARCHAR(20),@HIS_AP_OrderSheet_Id)
      ,transactionTime = @HIS_CurrentDate
      ,queueNumber     = @CallNum
      ,FPNum           = @HIS_FPNUM
      ,IsFinish        = 1
 WHERE Id = @Var_RegistId AND IsFinish=0

SELECT 1 AS Result,'成功' AS ResultText
      ,CHECKNAME
      ,CHECKPRICE
      ,CONVERT(VARCHAR(20),@HIS_AP_OrderSheet_Id) AS SourceNumber   
      ,CONVERT(VARCHAR(20),@HIS_AP_OrderSheet_Id) AS TransFlow      
      ,@HIS_CurrentDate                AS TransactionTime
      ,''                              AS CurrentNumber  
      ,@CallNum                        AS QueueNumber    
      ,@HIS_TimeInterval               AS ClinicRange    
      ,@HIS_PrePatientId               AS PatientId      
      ,@HIS_PatientName                AS Name           
      ,@HIS_Sex                        AS Sex            
      ,@HIS_DeptName                   AS DeptName       
      ,@HIS_Address                    AS Address        
      ,@HIS_DoctName                   AS DoctorName     
      ,@HIS_Duty                       AS Duty
 FROM MZCHECK (NOLOCK) WHERE FPNum = @HIS_FPNum